#include "ZombieSwarm.h"

void
ZombieSwarm::updateSwarmCenter() {

	OpenSteer::Vec3 swarmCenter(0, 0, 0);

	for (_zombiesIt = _zombies.begin();
		   _zombiesIt != _zombies.end();
		   _zombiesIt++)
		swarmCenter += (*_zombiesIt)->position();

	swarmCenter /= _zombies.size();

	setPosition(swarmCenter);

	_swarmCenter = swarmCenter;
}

void
ZombieSwarm::updateZombies(const float currentTime, const float elapsedTime) {

	for (_zombiesIt = _zombies.begin();
		   _zombiesIt != _zombies.end();
		   _zombiesIt++)
		(*_zombiesIt)->update(currentTime, elapsedTime);
}

ZombieSwarm::ZombieSwarm(
	OpenSteer::Vec3 position, int nZombies, Human* pHuman,
	ProximityDatabase& pd) {

	Zombie* pZombie;

	_initPosition = position;

	for (int n = 0; n < nZombies; n++) {

		pZombie = new Zombie(this, pHuman, pd);
		pZombie->setPosition(
			position + OpenSteer::randomVectorOnUnitRadiusXZDisk() * MAXDISTANCE);
		_zombies.push_back(pZombie);
	}
}

ZombieSwarm::~ZombieSwarm() { _zombies.clear(); }

void
ZombieSwarm::reset() {

	SimpleVehicle::reset();

	for (_zombiesIt = _zombies.begin();
		   _zombiesIt != _zombies.end();
		   _zombiesIt++) {

		(*_zombiesIt)->reset();
		(*_zombiesIt)->setPosition(
			_initPosition + OpenSteer::randomVectorOnUnitRadiusXZDisk() * MAXDISTANCE);
	}
}

void
ZombieSwarm::update(const float currentTime, const float elapsedTime) {

	updateSwarmCenter();
	updateZombies(currentTime, elapsedTime);
}

void
ZombieSwarm::draw() {

	drawXZCircle(MAXDISTANCE, _swarmCenter, OpenSteer::gGreen, 40);
	drawXZDisk(0.1f, _swarmCenter, OpenSteer::gGreen, 20);

	for (_zombiesIt = _zombies.begin();
	   _zombiesIt != _zombies.end();
	   _zombiesIt++)
	(*_zombiesIt)->draw();
}

OpenSteer::Vec3
ZombieSwarm::swarmCenter() const {

	return _swarmCenter;
}

